package ui.components;

import java.util.Enumeration;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.Dataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;

import entities.QueryResult;

public class PieChart extends Chart{
	
	public PieChart(String chartTitle) {
		super(chartTitle);		
	}

	public ChartPanel getChart() {
		return chartPanel;
	}

	/**
	 * Creates a sample dataset
	 */

	protected PieDataset createDataset(QueryResult qResult) {
		DefaultPieDataset result = new DefaultPieDataset();
		Enumeration<String> ptdos = qResult.getPartidos();
		while(ptdos.hasMoreElements()){
			String ptdo = ptdos.nextElement();
			result.setValue(ptdo+": "+qResult.getPtjeVoto(ptdo)+"%", qResult.getPtjeVoto(ptdo));
		}
		return result;
	}

	/**
	 * Creates a chart
	 */

	protected JFreeChart createChart(Dataset dataset, String chartTitle) {

		JFreeChart chart = ChartFactory.createPieChart3D(chartTitle, 
				(PieDataset) dataset, 
				true, 
				true, false);

		PiePlot plot = (PiePlot) chart.getPlot();
		plot.setStartAngle(290);
		plot.setDirection(Rotation.CLOCKWISE);
		plot.setForegroundAlpha(0.5f);
		return chart;

	}

	
}
